using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using RealDolmen.Controls;
using RealDolmen.SharePoint.ListFeatures.Elements;

namespace RealDolmen.SharePoint.ListFeatures
{
    public enum BuildPhase
    {
        OnLoad,
        CreateChildControls
    }
    public abstract class ListFeatureMenuItem : WebControl
    {
        #region Fields
        private Control menu;
        private List<ListFeatureCustomActionElement> customActions; 
        #endregion // Fields

        #region Properties
        public abstract string Location { get; }

        protected virtual BuildPhase BuildPhase
        {
            get
            {
                return BuildPhase.OnLoad;
            }
        }

        public virtual Control Menu
        {
            get
            {
                if (menu == null)
                    menu = ControlHelper.GetClosestParent<RepeatedControls>(this as Control);
                return menu;
            }
        } 
        #endregion // Properties

        #region Events
        // Man method to load the custom action
        protected override void OnInit(EventArgs e)
        {
            SPList list = SPContext.Current.List;
            ListViewWebPart lvw = ControlHelper.GetClosestParent<ListViewWebPart>(this);
            if (lvw != null)
            {
                Guid g = new Guid(lvw.ListName);
                list = SPContext.Current.Web.Lists[g];
            }

            if (list != null)
            {
                List<ListFeature> features =
                    ListFeature.GetActiveListFeatures(list);

                if (features.Count > 0)
                {
                    customActions = Core.GetElements<ListFeatureCustomActionElement>(features);
                }
            }
            Visible = false;
            base.OnInit(e);
        }

        private void ExtendMenu()
        {
            if (customActions != null && customActions.Count > 0)
            {
                bool hasControls = false;
                foreach (ListFeatureCustomActionElement customAction in customActions)
                {
                    if (customAction.Location == Location)
                    {
                        if (!hasControls)
                        {
                            BeforeAddControls();
                            hasControls = true;
                        }
                        AddControl(BuildControl(customAction), 0, customAction);
                    }
                }
                if(hasControls)
                    AfterAddControls();
            }
        }

        protected abstract Control BuildControl(ListFeatureCustomActionElement customAction);

        protected abstract void AddControl(Control ctrl, int index, ListFeatureCustomActionElement customAction);
        protected virtual void BeforeAddControls() { }
        protected virtual void AfterAddControls() { }

        protected override void OnLoad(EventArgs e)
        {
            // Previously this statement was in CreateChildControls
            if(BuildPhase == BuildPhase.OnLoad)
                ExtendMenu();

            base.OnLoad(e);
        }
        protected override void CreateChildControls()
        {
            if (BuildPhase == BuildPhase.CreateChildControls)
                ExtendMenu();
            base.CreateChildControls();
        }
        #endregion // Events
    }
}